package bma.common.langutil.core;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 分页器结果
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <TYPE>
 */
public class PagerResult<TYPE> {

	public PagerResult() {
		super();
	}

	public PagerResult(List<TYPE> result, Pager pager) {
		super();
		this.result = result;
		this.pager = pager;
	}

	private List<TYPE> result;

	private Pager pager;

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public List<TYPE> getResult() {
		return result;
	}

	public void setResult(List<TYPE> result) {
		this.result = result;
	}

	public void setResult(List resultList, Class<? extends TYPE> cls) {

		this.result = new LinkedList<TYPE>();
		if (resultList == null)
			return;
		for (Object obj : resultList) {
			result.add(cls.cast(obj));
		}
	}

	public <IN_TYPE> void setResult(List<IN_TYPE> resultList,
			Function<? super IN_TYPE, TYPE> co) {

		if (resultList == null) {
			this.result = new LinkedList<TYPE>();
			return;
		}
		this.result = ListUtil.toList(resultList, co);
	}

	public void setResult(Iterator resultList, Class<? extends TYPE> cls) {
		this.result = new LinkedList<TYPE>();
		if (resultList == null)
			return;
		while (resultList.hasNext()) {
			result.add(cls.cast(resultList.next()));
		}
	}

	public <IN_TYPE> void setResult(Iterator<IN_TYPE> resultList,
			Function<? super IN_TYPE, TYPE> co) {
		if (resultList == null) {
			this.result = new LinkedList<TYPE>();
			return;
		}
		this.result = ListUtil.toList(resultList, co);
	}

	public void create(List<TYPE> result, Pager pager) {
		this.result = result;
		this.pager = pager;
	}

	public <V> PagerResult<V> cast(Class<V> cls) {
		PagerResult<V> r = new PagerResult<V>();
		r.setPager(pager);
		r.setResult(this.result, cls);
		return r;
	}

	public <OUT_TYPE> PagerResult<OUT_TYPE> cast(
			Function<? super TYPE, OUT_TYPE> co) {
		PagerResult<OUT_TYPE> r = new PagerResult<OUT_TYPE>();
		r.setPager(pager);
		r.setResult(ListUtil.toList(this.result, co));
		return r;
	}

	@Override
	public String toString() {
		return "PR[" + pager + "," + result + "]";
	}
}
